package com.busines.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.validation.Valid;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.busines.dto.BaseDto;
import com.busines.dto.BaseDto.RetConstant;
import com.busines.dto.HelloDto;
import com.busines.pojo.xunting.TSchool;
import com.busines.practice.Knight;
import com.busines.service.IMepService;
import com.busines.service.IRedisService;
import com.busines.service.ISchoolAndStudent;
import com.busines.service.ITest;
import com.busines.vo.RegistVo;
import com.common.help.OutputExcelDemo;

@RestController
@RequestMapping("/test")
public class TestController {

	@Resource(name="mepService")
	private IMepService mepService;
	@Resource(name="schoolAndStudent")
	private ISchoolAndStudent schoolAndStudent;
	
	@Autowired
    private AmqpTemplate rabbitTemplate;
	
	@Autowired
	private Knight knight;
	
	@Autowired
	@Qualifier("testService")
	private ITest testService;
	
	@Resource(name="redisService")
	private IRedisService redisService;
	
	
	private final static Logger log  = LoggerFactory.getLogger(TestController.class);
	
	@RequestMapping(value="/test1")
	public BaseDto test1(@Valid RegistVo vo,BindingResult result){
		BaseDto dto = new BaseDto();
		if(result.hasErrors()){
			dto.setDefeaultMsgError(RetConstant.PARAM_ERROR.getValue(),result.getFieldError().getDefaultMessage());
			return dto;
		}
		System.out.println(mepService.countMep());
		return  dto;
	}
	
	@RequestMapping(value="/test2")
	public int test2(){
		
		return mepService.countXunting();
	}
	
	@RequestMapping(value="/test3")
	public List<TSchool> test3(){
		
		return schoolAndStudent.queryInformation();
	}
	
	@RequestMapping(value="/test4")
	public TSchool test4(Integer no){
		
		return schoolAndStudent.queryTSchool(no);
	}
	//到处excel
	@RequestMapping(value="/test5")
	public String test5(){
//		if(OutputExcelDemo.outputSchoolStudent()){
//			return "success";
//		}else{
			return "fail";
//		}
	}
	@RequestMapping(value="/test6")
	public BaseDto test6(){
		try {
			HelloDto hello = new HelloDto();
			hello.setCc("nihao");
			return hello;
		} catch (Exception e) {
			BaseDto dto = new BaseDto();
			e.printStackTrace();
			dto.serverMsgError();
			return dto;
		}
	}
	@RequestMapping(value="/test100")
	public void test100(){
		log.error("错误而已试试");
		rabbitTemplate.convertAndSend("fanoutExchange1", "", "看下三个队列都能收到吗？！");
	}
	
	@ResponseBody
	@RequestMapping("test111")
	public void test(){
		redisService.test2();
	}
	
	@ResponseBody
	@RequestMapping("test222")
	public void test222(){
		knight.embarkOnQuest();
	}
}
